home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / frasrc19.zip / FRACSUBR.C < prev    next >
C/C++ Source or Header  |  1995-03-04  |  43KB  |  1,403 lines

  1. /*
  2. FRACSUBR.C contains subroutines which belong primarily to CALCFRAC.C and
  3. FRACTALS.C, i.e. which are non-fractal-specific fractal engine subroutines.
  4. */
  5.  
  6. #include <stdio.h>
  7. #ifndef XFRACT
  8. #include <stdarg.h>
  9. #else
  10. #include <varargs.h>
  11. #endif
  12. #include <float.h>
  13. #include <sys/types.h>
  14. #include <sys/timeb.h>
  15. #include <stdlib.h>
  16. #include "fractint.h"
  17. #include "fractype.h"
  18. #include "mpmath.h"
  19. #include "prototyp.h"
  20.  
  21. /* routines in this module    */
  22.  
  23. static long   _fastcall fudgetolong(double d);
  24. static double _fastcall fudgetodouble(long l);
  25. static void   _fastcall adjust_to_limits(double);
  26. static void   _fastcall smallest_add(double *);
  27. static int    _fastcall ratio_bad(double,double);
  28. static void   _fastcall plotdorbit(double,double,int);
  29. static int    _fastcall combine_worklist(void);
  30.  
  31. static void   _fastcall adjust_to_limitsbf(double);
  32. static void   _fastcall smallest_add_bf(bf_t);
  33.        int    resume_len;        /* length of resume info */
  34. static int    resume_offset;        /* offset in resume info gets */
  35.  
  36.  
  37. #define FUDGEFACTOR    29    /* fudge all values up by 2**this */
  38. #define FUDGEFACTOR2    24    /* (or maybe this)          */
  39.  
  40. void fill_dx_array(void)
  41. {
  42.    int i;
  43.    dx0[0] = xxmin;        /* fill up the x, y grids */
  44.    dy0[0] = yymax;
  45.    dx1[0] = dy1[0] = 0;
  46.    for (i = 1; i < xdots; i++ ) {
  47.       dx0[i] = (double)(dx0[0] + i*delxx);
  48.       dy1[i] = (double)(dy1[0] - i*delyy2);
  49.    }
  50.    for (i = 1; i < ydots; i++ ) {
  51.       dy0[i] = (double)(dy0[0] - i*delyy);
  52.       dx1[i] = (double)(dx1[0] + i*delxx2);
  53.    }
  54. }
  55. void fill_lx_array(void)
  56. {
  57.    int i;
  58.    /* note that lx1 & ly1 values can overflow into sign bit; since     */
  59.    /* they're used only to add to lx0/ly0, 2s comp straightens it out  */
  60.    lx0[0] = xmin;         /* fill up the x, y grids */
  61.    ly0[0] = ymax;
  62.    lx1[0] = ly1[0] = 0;
  63.    for (i = 1; i < xdots; i++ ) {
  64.       lx0[i] = lx0[i-1] + delx;
  65.       ly1[i] = ly1[i-1] - dely2;
  66.    }
  67.    for (i = 1; i < ydots; i++ ) {
  68.       ly0[i] = ly0[i-1] - dely;
  69.       lx1[i] = lx1[i-1] + delx2;
  70.    }
  71. }
  72.  
  73. /* returns pointer to nonempty param string, NULL otherwise */
  74. char *typehasparm(int type,int parm)
  75. {
  76.    int extra;
  77.    char *ret = NULL;
  78.    if(0 <= parm && parm < 4)
  79.       ret=fractalspecific[type].param[parm];
  80.    else if(parm >= 4 && parm < MAXPARAMS)    
  81.       if((extra=find_extra_param(type)) > -1)
  82.          ret=moreparams[extra].param[parm-4];
  83.    if(ret)
  84.       if(*ret == 0)
  85.          ret = NULL;
  86.    return(ret);
  87. }
  88.  
  89. void fractal_floattobf(void)
  90. {
  91.    int i;
  92.    init_bf_dec(getprecdbl(CURRENTREZ));
  93.    floattobf(bfxmin,xxmin);
  94.    floattobf(bfxmax,xxmax);
  95.    floattobf(bfymin,yymin);
  96.    floattobf(bfymax,yymax);
  97.    floattobf(bfx3rd,xx3rd);
  98.    floattobf(bfy3rd,yy3rd);
  99.    
  100.    for (i = 0; i < MAXPARAMS; i++)
  101.       if(typehasparm(fractype,i)) 
  102.          floattobf(bfparms[i],param[i]);
  103.    calc_status = 0;
  104. }
  105.  
  106.  
  107. #ifdef _MSC_VER
  108. #if _MSC_VER == 800
  109. /* MSC8 doesn't correctly calculate the address of certain arrays here */
  110. #pragma optimize( "", off )
  111. #endif
  112. #endif
  113.  
  114. void calcfracinit(void) /* initialize a *pile* of stuff for fractal calculation */
  115. {
  116.    int i, gotprec;
  117.    double ftemp;
  118.    coloriter=oldcoloriter = 0L;
  119.    /* set up grid array compactly leaving space at end */
  120.    dx0 = MK_FP(extraseg,0);
  121.    dy1 = (dx1 = (dy0 = dx0 + xdots) + ydots) + ydots;
  122.    lx0 = (long far *) dx0;
  123.    ly1 = (lx1 = (ly0 = lx0 + xdots) + ydots) + ydots;
  124.    if(!(curfractalspecific->flags & BF_MATH))
  125.    {
  126.       int tofloat;
  127.       if((tofloat=curfractalspecific->tofloat) == NOFRACTAL)
  128.          bf_math = 0;
  129.       else if(!(fractalspecific[tofloat].flags & BF_MATH))
  130.          bf_math = 0;
  131.       else if(bf_math)
  132.       {
  133.          curfractalspecific = &fractalspecific[tofloat];
  134.          fractype = tofloat;
  135.       }
  136.    }
  137.    
  138.    /* switch back to double when zooming out if using arbitrary precision */
  139.    if(bf_math)
  140.    {
  141.       gotprec=getprecbf(CURRENTREZ);
  142.       if(gotprec <= DBL_DIG+1 && debugflag != 3200)
  143.       {
  144.          bfcornerstofloat(); 
  145.          bf_math = 0;
  146.       }
  147.       else
  148.          init_bf_dec(gotprec);
  149.    }
  150.    else if((fractype==MANDEL || fractype==MANDELFP) && debugflag==3200)
  151.    {
  152.       fractype=MANDELFP;
  153.       curfractalspecific = &fractalspecific[MANDELFP];   
  154.       fractal_floattobf();
  155.       usr_floatflag = 1;
  156.    }
  157.    else if((fractype==JULIA || fractype==JULIAFP) && debugflag==3200)
  158.    {
  159.       fractype=JULIAFP;
  160.       curfractalspecific = &fractalspecific[JULIAFP];   
  161.       fractal_floattobf();
  162.       usr_floatflag = 1;
  163.    }
  164.    else if((fractype==LMANDELZPOWER || fractype==FPMANDELZPOWER) && debugflag==3200)
  165.    {
  166.       fractype=FPMANDELZPOWER;
  167.       curfractalspecific = &fractalspecific[FPMANDELZPOWER];   
  168.       fractal_floattobf();
  169.       usr_floatflag = 1;
  170.    }   
  171.    else if((fractype==LJULIAZPOWER || fractype==FPJULIAZPOWER) && debugflag==3200)
  172.    {
  173.       fractype=FPJULIAZPOWER;
  174.       curfractalspecific = &fractalspecific[FPJULIAZPOWER];
  175.       fractal_floattobf();
  176.       usr_floatflag = 1;
  177.    }   
  178.    else
  179.       free_bf_vars();
  180.    if(bf_math)
  181.       floatflag=1;
  182.    else   
  183.       floatflag = usr_floatflag;
  184.    if (calc_status == 2) /* on resume, ensure floatflag correct */
  185.       if (curfractalspecific->isinteger)
  186.      floatflag = 0;
  187.       else
  188.      floatflag = 1;
  189.    /* if floating pt only, set floatflag for TAB screen */
  190.    if (!curfractalspecific->isinteger && curfractalspecific->tofloat == NOFRACTAL)
  191.       floatflag = 1;
  192.  
  193. init_restart:
  194.  
  195.    /* the following variables may be forced to a different setting due to
  196.       calc routine constraints;  usr_xxx is what the user last said is wanted,
  197.       xxx is what we actually do in the current situation */
  198.    stdcalcmode        = usr_stdcalcmode;
  199.    periodicitycheck = usr_periodicitycheck;
  200.    distest        = usr_distest;
  201.    biomorph        = usr_biomorph;
  202.  
  203.    potflag = 0;
  204.    if (potparam[0] != 0.0
  205.      && colors >= 64
  206.      && (curfractalspecific->calctype == StandardFractal
  207.      || curfractalspecific->calctype == calcmand
  208.      || curfractalspecific->calctype == calcmandfp)) {
  209.       potflag = 1;
  210.       distest = 0;    /* can't do distest too */
  211.       }
  212.  
  213.    if (distest)
  214.       floatflag = 1;  /* force floating point for dist est */
  215.  
  216.    if (floatflag) { /* ensure type matches floatflag */
  217.       if (curfractalspecific->isinteger != 0
  218.     && curfractalspecific->tofloat != NOFRACTAL)
  219.      fractype = curfractalspecific->tofloat;
  220.       }
  221.    else {
  222.       if (curfractalspecific->isinteger == 0
  223.     && curfractalspecific->tofloat != NOFRACTAL)
  224.      fractype = curfractalspecific->tofloat;
  225.       }
  226.    /* match Julibrot with integer mode of orbit */   
  227.    if(fractype == JULIBROTFP && fractalspecific[neworbittype].isinteger)
  228.    {
  229.       int i;
  230.       if((i=fractalspecific[neworbittype].tofloat) != NOFRACTAL)
  231.          neworbittype = i;
  232.       else
  233.          fractype = JULIBROT;   
  234.    }
  235.    else if(fractype == JULIBROT && fractalspecific[neworbittype].isinteger==0)
  236.    {
  237.       int i;
  238.       if((i=fractalspecific[neworbittype].tofloat) != NOFRACTAL)
  239.          neworbittype = i;
  240.       else
  241.          fractype = JULIBROTFP;   
  242.    }
  243.       
  244.    curfractalspecific = &fractalspecific[fractype];
  245.  
  246.    integerfractal = curfractalspecific->isinteger;
  247.  
  248. /*   if (fractype == JULIBROT)
  249.       rqlim = 4;
  250.    else */ if (potflag && potparam[2] != 0.0)
  251.       rqlim = potparam[2];
  252. /* else if (decomp[0] > 0 && decomp[1] > 0)
  253.       rqlim = (double)decomp[1]; */
  254.    else if (bailout) /* user input bailout */
  255.       rqlim = bailout;
  256.    else if (biomorph != -1) /* biomorph benefits from larger bailout */
  257.       rqlim = 100;
  258.    else
  259.       rqlim = curfractalspecific->orbit_bailout;
  260.    if (integerfractal) /* the bailout limit mustn't be too high here */
  261.       if (rqlim > 127.0) rqlim = 127.0;
  262.  
  263.    if ((curfractalspecific->flags&NOROTATE) != 0) {
  264.       /* ensure min<max and unrotated rectangle */
  265.       if (xxmin > xxmax) { ftemp = xxmax; xxmax = xxmin; xxmin = ftemp; }
  266.       if (yymin > yymax) { ftemp = yymax; yymax = yymin; yymin = ftemp; }
  267.       xx3rd = xxmin; yy3rd = yymin;
  268.       }
  269.  
  270.    /* set up bitshift for integer math */
  271.    bitshift = FUDGEFACTOR2; /* by default, the smaller shift */
  272.    if (integerfractal > 1)  /* use specific override from table */
  273.       bitshift = integerfractal;
  274.    if (integerfractal == 0) /* float? */
  275.       if ((i = curfractalspecific->tofloat) != NOFRACTAL) /* -> int? */
  276.       {
  277.      if (fractalspecific[i].isinteger > 1) /* specific shift? */
  278.         bitshift = fractalspecific[i].isinteger;
  279.       }
  280.       else
  281.          bitshift = 16;  /* to allow larger corners */
  282. /* We want this code if we're using the assembler calcmand */
  283.    if (fractype == MANDEL || fractype == JULIA) { /* adust shift bits if.. */
  284.       if (potflag == 0                  /* not using potential */
  285.     && (param[0] > -2.0 && param[0] < 2.0)  /* parameters not too large */
  286.     && (param[1] > -2.0 && param[1] < 2.0)
  287.     && !invert                  /* and not inverting */
  288.     && biomorph == -1              /* and not biomorphing */
  289.     && rqlim <= 4.0               /* and bailout not too high */
  290.     && (outside > -2 || outside < -6)      /* and no funny outside stuff */
  291.     && debugflag != 1234              /* and not debugging */
  292.     && bailoutest == Mod)              /* and bailout test = mod */
  293.      bitshift = FUDGEFACTOR;          /* use the larger bitshift */
  294.       }
  295.  
  296.    fudge = 1L << bitshift;
  297.  
  298.    l_at_rad = fudge/32768L;
  299.    f_at_rad = 1.0/32768L;
  300.  
  301.    /* now setup arrays of real coordinates corresponding to each pixel */
  302.    if(bf_math)
  303.       adjust_to_limitsbf(1.0); /* make sure all corners in valid range */
  304.    else
  305.    {
  306.       adjust_to_limits(1.0); /* make sure all corners in valid range */
  307.       delxx  = (LDBL)(xxmax - xx3rd) / (LDBL)dxsize; /* calculate stepsizes */
  308.       delyy  = (LDBL)(yymax - yy3rd) / (LDBL)dysize;
  309.       delxx2 = (LDBL)(xx3rd - xxmin) / (LDBL)dysize;
  310.       delyy2 = (LDBL)(yy3rd - yymin) / (LDBL)dxsize;
  311.       fill_dx_array();
  312.    }
  313.  
  314.    if(fractype != CELLULAR)  /* fudgetolong fails w >10 digits in double */
  315.    {
  316.       creal = fudgetolong(param[0]); /* integer equivs for it all */
  317.       cimag = fudgetolong(param[1]);
  318.       xmin  = fudgetolong(xxmin);
  319.       xmax  = fudgetolong(xxmax);
  320.       x3rd  = fudgetolong(xx3rd);
  321.       ymin  = fudgetolong(yymin);
  322.       ymax  = fudgetolong(yymax);
  323.       y3rd  = fudgetolong(yy3rd);
  324.       delx  = fudgetolong((double)delxx);
  325.       dely  = fudgetolong((double)delyy);
  326.       delx2 = fudgetolong((double)delxx2);
  327.       dely2 = fudgetolong((double)delyy2);
  328.    }
  329.  
  330.    /* skip this if plasma to avoid 3d problems */
  331.    /* skip if bf_math to avoid extraseg conflict with dx0 arrays */
  332.    if (fractype != PLASMA && bf_math == 0) 
  333.    {
  334.       if (integerfractal && !invert) 
  335.       {
  336.          if (    (delx  == 0 && delxx  != 0.0)
  337.          || (delx2 == 0 && delxx2 != 0.0)
  338.          || (dely  == 0 && delyy  != 0.0)
  339.          || (dely2 == 0 && delyy2 != 0.0) )
  340.         goto expand_retry;
  341.  
  342.          fill_lx_array();   /* fill up the x,y grids */
  343.      /* past max res?  check corners within 10% of expected */
  344.      if (    ratio_bad((double)lx0[xdots-1]-xmin,(double)xmax-x3rd)
  345.          || ratio_bad((double)ly0[ydots-1]-ymax,(double)y3rd-ymax)
  346.          || ratio_bad((double)lx1[(ydots>>1)-1],((double)x3rd-xmin)/2)
  347.          || ratio_bad((double)ly1[(xdots>>1)-1],((double)ymin-y3rd)/2) ) 
  348.          {
  349. expand_retry:
  350.         if (integerfractal        /* integer fractal type? */
  351.            && curfractalspecific->tofloat != NOFRACTAL)
  352.            floatflag = 1;        /* switch to floating pt */
  353.         else
  354.            adjust_to_limits(2.0);     /* double the size */
  355.         if (calc_status == 2)    /* due to restore of an old file? */
  356.            calc_status = 0;     /*   whatever, it isn't resumable */
  357.         goto init_restart;
  358.      } /* end if ratio bad */
  359.      
  360.      /* re-set corners to match reality */
  361.      xmax = lx0[xdots-1] + lx1[ydots-1];
  362.      ymin = ly0[ydots-1] + ly1[xdots-1];
  363.      x3rd = xmin + lx1[ydots-1];
  364.      y3rd = ly0[ydots-1];
  365.      xxmin = fudgetodouble(xmin);
  366.      xxmax = fudgetodouble(xmax);
  367.      xx3rd = fudgetodouble(x3rd);
  368.      yymin = fudgetodouble(ymin);
  369.      yymax = fudgetodouble(ymax);
  370.      yy3rd = fudgetodouble(y3rd);
  371.       } /* end if (integerfractal && !invert) */
  372.       else 
  373.       {
  374.      /* set up dx0 and dy0 analogs of lx0 and ly0 */
  375.      /* put fractal parameters in doubles */
  376.          dx0[0] = xxmin;        /* fill up the x, y grids */
  377.      dy0[0] = yymax;
  378.      dx1[0] = dy1[0] = 0;
  379.          /* this way of defining the dx and dy arrays is not the most 
  380.             accurate, but it is kept because it is used to determine
  381.             the limit of resolution */
  382.      for (i = 1; i < xdots; i++ ) {
  383.         dx0[i] = (double)(dx0[i-1] + delxx);
  384.         dy1[i] = (double)(dy1[i-1] - delyy2);
  385.         }
  386.      for (i = 1; i < ydots; i++ ) {
  387.         dy0[i] = (double)(dy0[i-1] - delyy);
  388.         dx1[i] = (double)(dx1[i-1] + delxx2);
  389.         }
  390.          if(bf_math == 0) /* redundant test, leave for now */
  391.          {
  392.         if (   ratio_bad(dx0[xdots-1]-xxmin,xxmax-xx3rd)
  393.                 || ratio_bad(dy0[ydots-1]-yymax,yy3rd-yymax)
  394.                 || ratio_bad(dx1[ydots-1],      xx3rd-xxmin)
  395.                 || ratio_bad(dy1[xdots-1],      yymin-yy3rd)) 
  396.         {
  397.                if(curfractalspecific->flags & BF_MATH)
  398.                {
  399.                   fractal_floattobf();
  400.                goto init_restart;
  401.            }   
  402.            goto expand_retry;
  403.         }
  404.      } /* end if bf_math == 0 */  
  405.  
  406.          /* if long double available, this is more accurate */
  407.      fill_dx_array();    /* fill up the x, y grids */
  408.  
  409.      /* re-set corners to match reality */
  410.      xxmax = dx0[xdots-1] + dx1[ydots-1];
  411.      yymin = dy0[ydots-1] + dy1[xdots-1];
  412.      xx3rd = xxmin + dx1[ydots-1];
  413.      yy3rd = dy0[ydots-1];
  414.       } /* end else */
  415.    } /* end if not plasma */
  416.  
  417.    /* for periodicity close-enough, and for unity: */
  418.    /*      min(max(delx,delx2),max(dely,dely2)       */
  419.    ddelmin = fabs((double)delxx);
  420.    if (fabs((double)delxx2) > ddelmin)
  421.       ddelmin = fabs((double)delxx2);
  422.    if (fabs((double)delyy) > fabs((double)delyy2)) 
  423.    {
  424.       if (fabs((double)delyy) < ddelmin)
  425.          ddelmin = fabs((double)delyy);
  426.    }
  427.    else if (fabs((double)delyy2) < ddelmin)
  428.       ddelmin = fabs((double)delyy2);
  429.    delmin = fudgetolong(ddelmin);
  430.  
  431.    /* calculate factors which plot real values to screen co-ords */
  432.    /* calcfrac.c plot_orbit routines have comments about this     */
  433.    ftemp = (double)((0.0-delyy2) * delxx2 * dxsize * dysize
  434.        - (xxmax-xx3rd) * (yy3rd-yymax));
  435.    if(ftemp != 0)
  436.    {
  437.       plotmx1 = (double)(delxx2 * dxsize * dysize / ftemp);
  438.       plotmx2 = (yy3rd-yymax) * dxsize / ftemp;
  439.       plotmy1 = (double)((0.0-delyy2) * dxsize * dysize / ftemp);
  440.       plotmy2 = (xxmax-xx3rd) * dysize / ftemp;
  441.    }
  442.    if(bf_math == 0)
  443.       free_bf_vars();
  444.    else
  445.    {
  446.       /* zap all of extraseg except high area to flush out bugs */
  447.       /* in production version this code can be deleted */
  448.       char far *extra;
  449.       extra = (char far *)MK_FP(extraseg,0);
  450.       far_memset(extra,0,(unsigned int)(0x10000l-(bflength+2)*22U));
  451.    }   
  452. }
  453.  
  454. #ifdef _MSC_VER
  455. #if _MSC_VER == 800
  456. #pragma optimize( "", on ) /* restore optimization options */
  457. #endif
  458. #endif
  459.  
  460. static long _fastcall fudgetolong(double d)
  461. {
  462.    if ((d *= fudge) > 0) d += 0.5;
  463.    else          d -= 0.5;
  464.    return (long)d;
  465. }
  466.  
  467. static double _fastcall fudgetodouble(long l)
  468. {
  469.    char buf[30];
  470.    double d;
  471.    sprintf(buf,"%.9g",(double)l / fudge);
  472. #ifndef XFRACT
  473.    sscanf(buf,"%lg",&d);
  474. #else
  475.    sscanf(buf,"%lf",&d);
  476. #endif
  477.    return d;
  478. }
  479.  
  480. void adjust_cornerbf(void)
  481. {
  482.    /* make edges very near vert/horiz exact, to ditch rounding errs and */
  483.    /* to avoid problems when delta per axis makes too large a ratio    */
  484.    bf_t bftemp, bftemp2;
  485.    bf_t btmp1;
  486.    int saved; saved = save_stack();
  487.    bftemp  = alloc_stack(rbflength+2);
  488.    bftemp2 = alloc_stack(rbflength+2);
  489.    btmp1  =  alloc_stack(rbflength+2);
  490.  
  491.    /* ftemp=fabs(xx3rd-xxmin); */
  492.    abs_a_bf(sub_bf(bftemp,bfx3rd,bfxmin)); 
  493.    
  494.    /* ftemp2=fabs(xxmax-xx3rd);*/
  495.    abs_a_bf(sub_bf(bftemp2,bfxmax,bfx3rd));                               
  496.  
  497.    /* if( (ftemp=fabs(xx3rd-xxmin)) < (ftemp2=fabs(xxmax-xx3rd)) ) */
  498.    if(cmp_bf(bftemp,bftemp2) < 0) 
  499.    {
  500.       /* if (ftemp*10000 < ftemp2 && yy3rd != yymax) */
  501.       if (cmp_bf(mult_bf_int(btmp1,bftemp,10000),bftemp2) < 0 
  502.          && cmp_bf(bfy3rd,bfymax) != 0 )
  503.      /* xx3rd = xxmin; */
  504.      copy_bf(bfx3rd, bfxmin);
  505.    }
  506.  
  507.    /* else if (ftemp2*10000 < ftemp && yy3rd != yymin) */
  508.    if (cmp_bf(mult_bf_int(btmp1,bftemp2,10000),bftemp) < 0 
  509.                    && cmp_bf(bfy3rd,bfymin) != 0 )
  510.       /* xx3rd = xxmax; */
  511.       copy_bf(bfx3rd, bfxmax);
  512.  
  513.    /* ftemp=fabs(yy3rd-yymin); */     
  514.    abs_a_bf(sub_bf(bftemp,bfy3rd,bfymin)); 
  515.  
  516.    /* ftemp2=fabs(yymax-yy3rd); */
  517.    abs_a_bf(sub_bf(bftemp2,bfymax,bfy3rd));
  518.     
  519.    /* if( (ftemp=fabs(yy3rd-yymin)) < (ftemp2=fabs(yymax-yy3rd)) ) */
  520.    if(cmp_bf(bftemp,bftemp2) < 0) 
  521.    {
  522.       /* if (ftemp*10000 < ftemp2 && xx3rd != xxmax) */
  523.       if (cmp_bf(mult_bf_int(btmp1,bftemp,10000),bftemp2) < 0 
  524.                  && cmp_bf(bfx3rd,bfxmax) != 0 )
  525.      /* yy3rd = yymin; */
  526.      copy_bf(bfy3rd, bfymin);
  527.    }
  528.  
  529.    /* else if (ftemp2*10000 < ftemp && xx3rd != xxmin) */
  530.      if (cmp_bf(mult_bf_int(btmp1,bftemp2,10000),bftemp) < 0 
  531.                       && cmp_bf(bfx3rd,bfxmin) != 0 )
  532.       /* yy3rd = yymax; */
  533.       copy_bf(bfy3rd, bfymax);
  534.  
  535.    restore_stack(saved);
  536. }
  537.  
  538. void adjust_corner(void)
  539. {
  540.    /* make edges very near vert/horiz exact, to ditch rounding errs and */
  541.    /* to avoid problems when delta per axis makes too large a ratio    */
  542.    double ftemp,ftemp2;
  543.  
  544.    if( (ftemp=fabs(xx3rd-xxmin)) < (ftemp2=fabs(xxmax-xx3rd)) ) {
  545.       if (ftemp*10000 < ftemp2 && yy3rd != yymax)
  546.      xx3rd = xxmin;
  547.       }
  548.  
  549.    if (ftemp2*10000 < ftemp && yy3rd != yymin)
  550.       xx3rd = xxmax;
  551.  
  552.  
  553.    if( (ftemp=fabs(yy3rd-yymin)) < (ftemp2=fabs(yymax-yy3rd)) ) {
  554.       if (ftemp*10000 < ftemp2 && xx3rd != xxmax)
  555.      yy3rd = yymin;
  556.       }
  557.  
  558.    if (ftemp2*10000 < ftemp && xx3rd != xxmin)
  559.       yy3rd = yymax;
  560.  
  561. }
  562.  
  563. static void _fastcall adjust_to_limitsbf(double expand)
  564. {  
  565.    LDBL limit;
  566.    bf_t bcornerx[4],bcornery[4];
  567.    bf_t blowx,bhighx,blowy,bhighy,blimit,bftemp;
  568.    bf_t bcenterx,bcentery,badjx,badjy,btmp1,btmp2;
  569.    bf_t bexpand;
  570.    int i;
  571.    int saved; saved = save_stack();
  572.    bcornerx[0] = alloc_stack(rbflength+2);
  573.    bcornerx[1] = alloc_stack(rbflength+2);
  574.    bcornerx[2] = alloc_stack(rbflength+2);
  575.    bcornerx[3] = alloc_stack(rbflength+2);
  576.    bcornery[0] = alloc_stack(rbflength+2);
  577.    bcornery[1] = alloc_stack(rbflength+2);
  578.    bcornery[2] = alloc_stack(rbflength+2);
  579.    bcornery[3] = alloc_stack(rbflength+2);
  580.    blowx       = alloc_stack(rbflength+2);
  581.    bhighx      = alloc_stack(rbflength+2);
  582.    blowy       = alloc_stack(rbflength+2);
  583.    bhighy      = alloc_stack(rbflength+2);
  584.    blimit      = alloc_stack(rbflength+2);
  585.    bftemp      = alloc_stack(rbflength+2);
  586.    bcenterx    = alloc_stack(rbflength+2);
  587.    bcentery    = alloc_stack(rbflength+2);
  588.    badjx       = alloc_stack(rbflength+2);
  589.    badjy       = alloc_stack(rbflength+2);
  590.    btmp1       = alloc_stack(rbflength+2);
  591.    btmp2       = alloc_stack(rbflength+2);
  592.    bexpand     = alloc_stack(rbflength+2);
  593.  
  594.    limit = 32767.99;
  595.  
  596.    if (bitshift >= 24) limit = 31.99;
  597.    if (bitshift >= 29) limit = 3.99;
  598.    floattobf(blimit,limit);
  599.    floattobf(bexpand,expand);
  600.    
  601.    add_bf(bcenterx,bfxmin,bfxmax);
  602.    half_a_bf(bcenterx);
  603.  
  604.    /* centery = (yymin+yymax)/2; */
  605.    add_bf(bcentery,bfymin,bfymax);
  606.    half_a_bf(bcentery);
  607.       
  608.    /* if (xxmin == centerx) { */
  609.    if (cmp_bf(bfxmin,bcenterx)==0) { /* ohoh, infinitely thin, fix it */
  610.       smallest_add_bf(bfxmax);
  611.       /* bfxmin -= bfxmax-centerx; */
  612.       sub_a_bf(bfxmin,sub_bf(btmp1,bfxmax,bcenterx));
  613.       }
  614.  
  615.    /* if (bfymin == centery) */
  616.    if (cmp_bf(bfymin,bcentery)==0) { 
  617.       smallest_add_bf(bfymax);
  618.       /* bfymin -= bfymax-centery; */
  619.       sub_a_bf(bfymin,sub_bf(btmp1,bfymax,bcentery));
  620.       }
  621.  
  622.    /* if (bfx3rd == centerx) */
  623.    if (cmp_bf(bfx3rd,bcenterx)==0)  
  624.       smallest_add_bf(bfx3rd);
  625.  
  626.    /* if (bfy3rd == centery) */
  627.    if (cmp_bf(bfy3rd,bcentery)==0)  
  628.       smallest_add_bf(bfy3rd);
  629.  
  630.    /* setup array for easier manipulation */
  631.    /* cornerx[0] = xxmin; */ 
  632.    copy_bf(bcornerx[0],bfxmin); 
  633.  
  634.    /* cornerx[1] = xxmax; */
  635.    copy_bf(bcornerx[1],bfxmax);
  636.  
  637.    /* cornerx[2] = xx3rd; */ 
  638.    copy_bf(bcornerx[2],bfx3rd);
  639.  
  640.    /* cornerx[3] = xxmin+(xxmax-xx3rd); */
  641.    sub_bf(bcornerx[3],bfxmax,bfx3rd);   
  642.    add_a_bf(bcornerx[3],bfxmin);   
  643.  
  644.    /* cornery[0] = yymax; */ 
  645.    copy_bf(bcornery[0],bfymax); 
  646.  
  647.    /* cornery[1] = yymin; */
  648.    copy_bf(bcornery[1],bfymin);
  649.    
  650.    /* cornery[2] = yy3rd; */ 
  651.    copy_bf(bcornery[2],bfy3rd); 
  652.    
  653.    /* cornery[3] = yymin+(yymax-yy3rd); */
  654.    sub_bf(bcornery[3],bfymax,bfy3rd);   
  655.    add_a_bf(bcornery[3],bfymin);   
  656.  
  657.    /* if caller wants image size adjusted, do that first */
  658.    if (expand != 1.0)
  659.    {
  660.       for (i=0; i<4; ++i) {
  661.      /* cornerx[i] = centerx + (cornerx[i]-centerx)*expand; */
  662.      sub_bf(btmp1,bcornerx[i],bcenterx);
  663.      mult_bf(bcornerx[i],btmp1,bexpand);
  664.      add_a_bf(bcornerx[i],bcenterx);
  665.  
  666.      /* cornery[i] = centery + (cornery[i]-centery)*expand; */
  667.      sub_bf(btmp1,bcornery[i],bcentery);
  668.      mult_bf(bcornery[i],btmp1,bexpand);
  669.      add_a_bf(bcornery[i],bcentery);
  670.       }
  671.    }
  672.  
  673.    /* get min/max x/y values */
  674.    /* lowx = highx = cornerx[0]; */
  675.    copy_bf(blowx,bcornerx[0]); copy_bf(bhighx,bcornerx[0]);   
  676.  
  677.    /* lowy = highy = cornery[0]; */
  678.    copy_bf(blowy,bcornery[0]); copy_bf(bhighy,bcornery[0]);   
  679.  
  680.    for (i=1; i<4; ++i) {
  681.       /* if (cornerx[i] < lowx)               lowx  = cornerx[i]; */
  682.       if (cmp_bf(bcornerx[i],blowx) < 0)   copy_bf(blowx,bcornerx[i]);
  683.  
  684.       /* if (cornerx[i] > highx)              highx = cornerx[i]; */
  685.       if (cmp_bf(bcornerx[i],bhighx) > 0)  copy_bf(bhighx,bcornerx[i]);
  686.  
  687.       /* if (cornery[i] < lowy)               lowy  = cornery[i]; */
  688.       if (cmp_bf(bcornery[i],blowy) < 0)   copy_bf(blowy,bcornery[i]);
  689.  
  690.       /* if (cornery[i] > highy)              highy = cornery[i]; */
  691.       if (cmp_bf(bcornery[i],bhighy) > 0)  copy_bf(bhighy,bcornery[i]);
  692.       }
  693.  
  694.    /* if image is too large, downsize it maintaining center */
  695.    /* ftemp = highx-lowx; */
  696.    sub_bf(bftemp,bhighx,blowx);
  697.    
  698.    /* if (highy-lowy > ftemp) ftemp = highy-lowy; */
  699.    if (cmp_bf(sub_bf(btmp1,bhighy,blowy),bftemp) > 0) copy_bf(bftemp,btmp1);
  700.  
  701.    /* if image is too large, downsize it maintaining center */
  702.  
  703.    floattobf(btmp1,limit*2.0);
  704.    copy_bf(btmp2,bftemp);
  705.    div_bf(bftemp,btmp1,btmp2);
  706.    floattobf(btmp1,1.0);
  707.    if (cmp_bf(bftemp,btmp1) < 0) 
  708.       for (i=0; i<4; ++i) {
  709.      /* cornerx[i] = centerx + (cornerx[i]-centerx)*ftemp; */
  710.      sub_bf(btmp1,bcornerx[i],bcenterx);
  711.      mult_bf(bcornerx[i],btmp1,bftemp);
  712.      add_a_bf(bcornerx[i],bcenterx);
  713.      
  714.      /* cornery[i] = centery + (cornery[i]-centery)*ftemp; */
  715.      sub_bf(btmp1,bcornery[i],bcentery);
  716.      mult_bf(bcornery[i],btmp1,bftemp);
  717.      add_a_bf(bcornery[i],bcentery);
  718.      }
  719.  
  720.    /* if any corner has x or y past limit, move the image */
  721.    /* adjx = adjy = 0; */
  722.    clear_bf(badjx); clear_bf(badjy);
  723.    
  724.    for (i=0; i<4; ++i) {
  725.       /* if (cornerx[i] > limit && (ftemp = cornerx[i] - limit) > adjx)
  726.      adjx = ftemp; */
  727.       if (cmp_bf(bcornerx[i],blimit) > 0 && 
  728.           cmp_bf(sub_bf(bftemp,bcornerx[i],blimit),badjx) > 0)
  729.      copy_bf(badjx,bftemp);
  730.  
  731.       /* if (cornerx[i] < 0.0-limit && (ftemp = cornerx[i] + limit) < adjx)
  732.      adjx = ftemp; */
  733.       if (cmp_bf(bcornerx[i],neg_bf(btmp1,blimit)) < 0 && 
  734.           cmp_bf(add_bf(bftemp,bcornerx[i],blimit),badjx) < 0)
  735.      copy_bf(badjx,bftemp);
  736.  
  737.       /* if (cornery[i] > limit     && (ftemp = cornery[i] - limit) > adjy)
  738.      adjy = ftemp; */
  739.       if (cmp_bf(bcornery[i],blimit) > 0 && 
  740.           cmp_bf(sub_bf(bftemp,bcornery[i],blimit),badjy) > 0)
  741.      copy_bf(badjy,bftemp);
  742.  
  743.       /* if (cornery[i] < 0.0-limit && (ftemp = cornery[i] + limit) < adjy)
  744.      adjy = ftemp; */
  745.       if (cmp_bf(bcornery[i],neg_bf(btmp1,blimit)) < 0 && 
  746.           cmp_bf(add_bf(bftemp,bcornery[i],blimit),badjy) < 0)
  747.      copy_bf(badjy,bftemp);
  748.       }
  749.  
  750.    /* if (calc_status == 2 && (adjx != 0 || adjy != 0) && (zwidth == 1.0))
  751.       calc_status = 0; */
  752.    if (calc_status == 2 && (is_bf_not_zero(badjx)|| is_bf_not_zero(badjy)) && (zwidth == 1.0))
  753.       calc_status = 0;
  754.  
  755.    /* xxmin = cornerx[0] - adjx; */
  756.    sub_bf(bfxmin,bcornerx[0],badjx);
  757.    /* xxmax = cornerx[1] - adjx; */
  758.    sub_bf(bfxmax,bcornerx[1],badjx);
  759.    /* xx3rd = cornerx[2] - adjx; */
  760.    sub_bf(bfx3rd,bcornerx[2],badjx);
  761.    /* yymax = cornery[0] - adjy; */
  762.    sub_bf(bfymax,bcornery[0],badjy);
  763.    /* yymin = cornery[1] - adjy; */
  764.    sub_bf(bfymin,bcornery[1],badjy);
  765.    /* yy3rd = cornery[2] - adjy; */
  766.    sub_bf(bfy3rd,bcornery[2],badjy);
  767.  
  768.    adjust_cornerbf(); /* make 3rd corner exact if very near other co-ords */
  769.    restore_stack(saved);
  770. }
  771.  
  772. static void _fastcall adjust_to_limits(double expand)
  773. {  
  774.    double cornerx[4],cornery[4];
  775.    double lowx,highx,lowy,highy,limit,ftemp;
  776.    double centerx,centery,adjx,adjy;
  777.    int i;
  778.  
  779.    limit = 32767.99;
  780.  
  781.    if (bitshift >= 24) limit = 31.99;
  782.    if (bitshift >= 29) limit = 3.99;
  783.  
  784.    centerx = (xxmin+xxmax)/2;
  785.    centery = (yymin+yymax)/2;
  786.       
  787.    if (xxmin == centerx) { /* ohoh, infinitely thin, fix it */
  788.       smallest_add(&xxmax);
  789.       xxmin -= xxmax-centerx;
  790.       }
  791.  
  792.    if (yymin == centery) {
  793.       smallest_add(&yymax);
  794.       yymin -= yymax-centery;
  795.       }
  796.  
  797.    if (xx3rd == centerx)
  798.       smallest_add(&xx3rd);
  799.  
  800.    if (yy3rd == centery)
  801.       smallest_add(&yy3rd);
  802.  
  803.    /* setup array for easier manipulation */
  804.    cornerx[0] = xxmin; 
  805.    cornerx[1] = xxmax;
  806.    cornerx[2] = xx3rd; 
  807.    cornerx[3] = xxmin+(xxmax-xx3rd);
  808.  
  809.    cornery[0] = yymax; 
  810.    cornery[1] = yymin;
  811.    cornery[2] = yy3rd; 
  812.    cornery[3] = yymin+(yymax-yy3rd);
  813.  
  814.    /* if caller wants image size adjusted, do that first */
  815.    if (expand != 1.0)
  816.    {
  817.       for (i=0; i<4; ++i) {
  818.      cornerx[i] = centerx + (cornerx[i]-centerx)*expand;
  819.      cornery[i] = centery + (cornery[i]-centery)*expand;
  820.       }
  821.    }
  822.    /* get min/max x/y values */
  823.    lowx = highx = cornerx[0];
  824.    lowy = highy = cornery[0];
  825.  
  826.    for (i=1; i<4; ++i) {
  827.       if (cornerx[i] < lowx)               lowx  = cornerx[i];
  828.       if (cornerx[i] > highx)              highx = cornerx[i];
  829.       if (cornery[i] < lowy)               lowy  = cornery[i];
  830.       if (cornery[i] > highy)              highy = cornery[i];
  831.       }
  832.  
  833.    /* if image is too large, downsize it maintaining center */
  834.    ftemp = highx-lowx;
  835.    
  836.    if (highy-lowy > ftemp) ftemp = highy-lowy;
  837.  
  838.    /* if image is too large, downsize it maintaining center */
  839.    if ((ftemp = limit*2/ftemp) < 1.0) {
  840.       for (i=0; i<4; ++i) {
  841.      cornerx[i] = centerx + (cornerx[i]-centerx)*ftemp;
  842.      cornery[i] = centery + (cornery[i]-centery)*ftemp;
  843.      }
  844.    }
  845.  
  846.    /* if any corner has x or y past limit, move the image */
  847.    adjx = adjy = 0;
  848.    
  849.    for (i=0; i<4; ++i) {
  850.       if (cornerx[i] > limit && (ftemp = cornerx[i] - limit) > adjx)
  851.      adjx = ftemp;
  852.       if (cornerx[i] < 0.0-limit && (ftemp = cornerx[i] + limit) < adjx)
  853.      adjx = ftemp;
  854.       if (cornery[i] > limit     && (ftemp = cornery[i] - limit) > adjy)
  855.      adjy = ftemp;
  856.       if (cornery[i] < 0.0-limit && (ftemp = cornery[i] + limit) < adjy)
  857.      adjy = ftemp;
  858.       }
  859.    if (calc_status == 2 && (adjx != 0 || adjy != 0) && (zwidth == 1.0))
  860.       calc_status = 0;
  861.    xxmin = cornerx[0] - adjx;
  862.    xxmax = cornerx[1] - adjx;
  863.    xx3rd = cornerx[2] - adjx;
  864.    yymax = cornery[0] - adjy;
  865.    yymin = cornery[1] - adjy;
  866.    yy3rd = cornery[2] - adjy;
  867.  
  868.    adjust_corner(); /* make 3rd corner exact if very near other co-ords */
  869. }
  870.  
  871. static void _fastcall smallest_add(double *num)
  872. {
  873.    *num += *num * 5.0e-16;
  874. }
  875.  
  876. static void _fastcall smallest_add_bf(bf_t num)
  877. {
  878.    bf_t btmp1;
  879.    int saved; saved = save_stack();
  880.    btmp1 = alloc_stack(bflength+2);
  881.    mult_bf(btmp1,floattobf(btmp1, 5.0e-16),num);
  882.    add_a_bf(num,btmp1);
  883.    restore_stack(saved);
  884. }
  885.  
  886. static int _fastcall ratio_bad(double actual, double desired)
  887. {  double ftemp;
  888.    if (desired != 0 && debugflag != 3400)
  889.       if ((ftemp = actual / desired) < 0.95 || ftemp > 1.05)
  890.      return(1);
  891.    return(0);
  892. }
  893.  
  894.  
  895. /* Save/resume stuff:
  896.  
  897.    Engines which aren't resumable can simply ignore all this.
  898.  
  899.    Before calling the (per_image,calctype) routines (engine), calcfract sets:
  900.       "resuming" to 0 if new image, nonzero if resuming a partially done image
  901.       "calc_status" to 1
  902.    If an engine is interrupted and wants to be able to resume it must:
  903.       store whatever status info it needs to be able to resume later
  904.       set calc_status to 2 and return
  905.    If subsequently called with resuming!=0, the engine must restore status
  906.    info and continue from where it left off.
  907.  
  908.    Since the info required for resume can get rather large for some types,
  909.    it is not stored directly in save_info.  Instead, memory is dynamically
  910.    allocated as required, and stored in .fra files as a separate block.
  911.    To save info for later resume, an engine routine can use:
  912.       alloc_resume(maxsize,version)
  913.      Maxsize must be >= max bytes subsequently saved + 2; over-allocation
  914.      is harmless except for possibility of insufficient mem available;
  915.      undersize is not checked and probably causes serious misbehaviour.
  916.      Version is an arbitrary number so that subsequent revisions of the
  917.      engine can be made backward compatible.
  918.      Alloc_resume sets calc_status to 2 (resumable) if it succeeds; to 3
  919.      if it cannot allocate memory (and issues warning to user).
  920.       put_resume({bytes,&argument,} ... 0)
  921.      Can be called as often as required to store the info.
  922.      Arguments must not be far addresses.
  923.      Is not protected against calls which use more space than allocated.
  924.    To reload info when resuming, use:
  925.       start_resume()
  926.      initializes and returns version number
  927.       get_resume({bytes,&argument,} ... 0)
  928.      inverse of store_resume
  929.       end_resume()
  930.      optional, frees the memory area sooner than would happen otherwise
  931.  
  932.    Example, save info:
  933.       alloc_resume(sizeof(parmarray)+100,2);
  934.       put_resume(sizeof(int),&row, sizeof(int),&col,
  935.          sizeof(parmarray),parmarray, 0);
  936.     restore info:
  937.       vsn = start_resume();
  938.       get_resume(sizeof(int),&row, sizeof(int),&col, 0);
  939.       if (vsn >= 2)
  940.      get_resume(sizeof(parmarray),parmarray,0);
  941.       end_resume();
  942.  
  943.    Engines which allocate a large far memory chunk of their own might
  944.    directly set resume_info, resume_len, calc_status to avoid doubling
  945.    transient memory needs by using these routines.
  946.  
  947.    StandardFractal, calcmand, solidguess, and bound_trace_main are a related
  948.    set of engines for escape-time fractals.  They use a common worklist
  949.    structure for save/resume.  Fractals using these must specify calcmand
  950.    or StandardFractal as the engine in fractalspecificinfo.
  951.    Other engines don't get btm nor ssg, don't get off-axis symmetry nor
  952.    panning (the worklist stuff), and are on their own for save/resume.
  953.  
  954.    */
  955.  
  956. #ifndef XFRACT
  957. int put_resume(int len, ...)
  958. #else
  959. int put_resume(va_alist)
  960. va_dcl
  961. #endif
  962. {
  963.    va_list arg_marker;    /* variable arg list */
  964.    char *source_ptr;
  965. #ifdef XFRACT
  966.    int len;
  967. #endif
  968.  
  969.    if (resume_info == NULL)
  970.       return(-1);
  971. #ifndef XFRACT
  972.    va_start(arg_marker,len);
  973. #else
  974.    va_start(arg_marker);
  975.    len = va_arg(arg_marker,int);
  976. #endif
  977.    while (len)
  978.    {
  979.       source_ptr = va_arg(arg_marker,char *);
  980.       far_memcpy(resume_info+resume_len,source_ptr,len);
  981.       resume_len += len;
  982.       len = va_arg(arg_marker,int);
  983.    }
  984.    return(0);
  985. }
  986.  
  987. int alloc_resume(int alloclen, int version)
  988. {
  989.    if (resume_info != NULL) /* free the prior area if there is one */
  990.       farmemfree(resume_info);
  991.    if ((resume_info = farmemalloc((long)alloclen))== NULL)
  992.    {
  993.       static FCODE msg[] = {"\
  994. Warning - insufficient free memory to save status.\n\
  995. You will not be able to resume calculating this image."};
  996.       stopmsg(0,msg);
  997.       calc_status = 3;
  998.       return(-1);
  999.    }
  1000.    resume_len = 0;
  1001.    put_resume(sizeof(int),&version,0);
  1002.    calc_status = 2;
  1003.    return(0);
  1004. }
  1005.  
  1006. #ifndef XFRACT
  1007. int get_resume(int len, ...)
  1008. #else
  1009. int get_resume(va_alist)
  1010. va_dcl
  1011. #endif
  1012. {
  1013.    va_list arg_marker;    /* variable arg list */
  1014.    char *dest_ptr;
  1015. #ifdef XFRACT
  1016.    int len;
  1017. #endif
  1018.  
  1019.    if (resume_info == NULL)
  1020.       return(-1);
  1021. #ifndef XFRACT
  1022.    va_start(arg_marker,len);
  1023. #else
  1024.    va_start(arg_marker);
  1025.    len = va_arg(arg_marker,int);
  1026. #endif
  1027.    while (len)
  1028.    {
  1029.       dest_ptr = va_arg(arg_marker,char *);
  1030.       far_memcpy(dest_ptr,resume_info+resume_offset,len);
  1031.       resume_offset += len;
  1032.       len = va_arg(arg_marker,int);
  1033.    }
  1034.    return(0);
  1035. }
  1036.  
  1037. int start_resume(void)
  1038. {
  1039.    int version;
  1040.    if (resume_info == NULL)
  1041.       return(-1);
  1042.    resume_offset = 0;
  1043.    get_resume(sizeof(int),&version,0);
  1044.    return(version);
  1045. }
  1046.  
  1047. void end_resume(void)
  1048. {
  1049.    if (resume_info != NULL) /* free the prior area if there is one */
  1050.    {
  1051.       farmemfree(resume_info);
  1052.       resume_info = NULL;
  1053.    }
  1054. }
  1055.  
  1056.  
  1057. /* Showing orbit requires converting real co-ords to screen co-ords.
  1058.    Define:
  1059.        Xs == xxmax-xx3rd           Ys == yy3rd-yymax
  1060.        W  == xdots-1               D  == ydots-1
  1061.    We know that:
  1062.        realx == lx0[col] + lx1[row]
  1063.        realy == ly0[row] + ly1[col]
  1064.        lx0[col] == (col/width) * Xs + xxmin
  1065.        lx1[row] == row * delxx
  1066.        ly0[row] == (row/D) * Ys + yymax
  1067.        ly1[col] == col * (0-delyy)
  1068.   so:
  1069.        realx == (col/W) * Xs + xxmin + row * delxx
  1070.        realy == (row/D) * Ys + yymax + col * (0-delyy)
  1071.   and therefore:
  1072.        row == (realx-xxmin - (col/W)*Xs) / Xv     (1)
  1073.        col == (realy-yymax - (row/D)*Ys) / Yv     (2)
  1074.   substitute (2) into (1) and solve for row:
  1075.        row == ((realx-xxmin)*(0-delyy2)*W*D - (realy-yymax)*Xs*D)
  1076.               / ((0-delyy2)*W*delxx2*D-Ys*Xs)
  1077.   */
  1078.  
  1079. /* sleep N * a tenth of a millisecond */
  1080.  
  1081. void sleepms(long ms)
  1082. {
  1083.     static long scalems = 0L;
  1084.     int savehelpmode,savetabmode;
  1085.     struct timeb t1,t2;
  1086. #define SLEEPINIT 250 /* milliseconds for calibration */
  1087.     savetabmode  = tabmode;
  1088.     savehelpmode = helpmode;
  1089.     tabmode  = 0;
  1090.     helpmode = -1;
  1091.     if(scalems==0L) /* calibrate */
  1092.     {
  1093.     /* selects a value of scalems that makes the units
  1094.        10000 per sec independent of CPU speed */
  1095.     int i,elapsed;
  1096.     scalems = 1L;
  1097.     if(keypressed()) /* check at start, hope to get start of timeslice */
  1098.        goto sleepexit;
  1099.     /* calibrate, assume slow computer first */
  1100.         showtempmsg("Calibrating timer");
  1101.     do
  1102.     {
  1103.        scalems *= 2;
  1104.        ftime(&t2);
  1105.        do { /* wait for the start of a new tick */
  1106.           ftime(&t1);
  1107.         }
  1108.         while (t2.time == t1.time && t2.millitm == t1.millitm);
  1109.        sleepms(10L * SLEEPINIT); /* about 1/4 sec */
  1110.        ftime(&t2);
  1111.        if(keypressed()) {
  1112.           scalems = 0L;
  1113.           goto sleepexit;
  1114.        }
  1115.      }
  1116.      while ((elapsed = (int)(t2.time-t1.time)*1000 + t2.millitm-t1.millitm)
  1117.         < SLEEPINIT);
  1118.     /* once more to see if faster (eg multi-tasking) */
  1119.     do { /* wait for the start of a new tick */
  1120.        ftime(&t1);
  1121.        }
  1122.      while (t2.time == t1.time && t2.millitm == t1.millitm);
  1123.     sleepms(10L * SLEEPINIT);
  1124.     ftime(&t2);
  1125.     if ((i = (int)(t2.time-t1.time)*1000 + t2.millitm-t1.millitm) < elapsed)
  1126.        elapsed = (i == 0) ? 1 : i;
  1127.     scalems = (long)((float)SLEEPINIT/(float)(elapsed) * scalems);
  1128. #if 0
  1129.     char msg[80];
  1130.     if (debugflag == 700) {
  1131.        sprintf(msg,"scale factor=%ld",scalems);
  1132.        stopmsg(0,msg);
  1133.     }
  1134. #endif
  1135.         cleartempmsg();
  1136.     }
  1137.     if(ms > 10L * SLEEPINIT) { /* using ftime is probably more accurate */
  1138.     ms /= 10;
  1139.     ftime(&t1);
  1140.     for(;;) {
  1141.        if(keypressed()) break;
  1142.        ftime(&t2);
  1143.        if ((long)((t2.time-t1.time)*1000 + t2.millitm-t1.millitm) >= ms) break;
  1144.     }
  1145.     }
  1146.     else
  1147.     if(!keypressed()) {
  1148.        ms *= scalems;
  1149.        while(ms-- >= 0);
  1150.     }
  1151. sleepexit:
  1152.     tabmode  = savetabmode;
  1153.     helpmode = savehelpmode;
  1154. }
  1155.  
  1156.  
  1157. static void _fastcall plotdorbit(double dx, double dy, int color)
  1158. {
  1159.    int i, j, c;
  1160.    int save_sxoffs,save_syoffs;
  1161.    if (orbit_ptr >= 1500) return;
  1162.    i = (int)(dy * plotmx1 - dx * plotmx2); i += sxoffs;
  1163.    if (i < 0 || i >= sxdots) return;
  1164.    j = (int)(dx * plotmy1 - dy * plotmy2); j += syoffs;
  1165.    if (j < 0 || j >= sydots) return;
  1166.    save_sxoffs = sxoffs;
  1167.    save_syoffs = syoffs;
  1168.    sxoffs = syoffs = 0;
  1169.    /* save orbit value */
  1170.    if(color == -1)
  1171.    {
  1172.       *(save_orbit + orbit_ptr++) = i;
  1173.       *(save_orbit + orbit_ptr++) = j;
  1174.       *(save_orbit + orbit_ptr++) = c = getcolor(i,j);
  1175.       putcolor(i,j,c^orbit_color);
  1176.    }
  1177.    else
  1178.       putcolor(i,j,color);
  1179.    sxoffs = save_sxoffs;
  1180.    syoffs = save_syoffs;
  1181.    if(orbit_delay > 0)
  1182.       sleepms(orbit_delay);
  1183.    if(soundflag==1)
  1184.        snd((int)(i*1000/xdots+basehertz));
  1185.    else if(soundflag > 1)
  1186.        snd((int)(j*1000/ydots+basehertz));
  1187.    /* placing sleepms here delays each dot */
  1188. }
  1189.  
  1190. void iplot_orbit(long ix, long iy, int color)
  1191. {
  1192.    plotdorbit((double)ix/fudge-xxmin,(double)iy/fudge-yymax,color);
  1193. }
  1194.  
  1195. void plot_orbit(double real,double imag,int color)
  1196. {
  1197.    plotdorbit(real-xxmin,imag-yymax,color);
  1198. }
  1199.  
  1200. void scrub_orbit(void)
  1201. {
  1202.    int i,j,c;
  1203.    int save_sxoffs,save_syoffs;
  1204.    save_sxoffs = sxoffs;
  1205.    save_syoffs = syoffs;
  1206.    sxoffs = syoffs = 0;
  1207.    while(orbit_ptr > 0)
  1208.    {
  1209.       c = *(save_orbit + --orbit_ptr);
  1210.       j = *(save_orbit + --orbit_ptr);
  1211.       i = *(save_orbit + --orbit_ptr);
  1212.       putcolor(i,j,c);
  1213.    }
  1214.    sxoffs = save_sxoffs;
  1215.    syoffs = save_syoffs;
  1216.    nosnd();
  1217. }
  1218.  
  1219.  
  1220. int add_worklist(int xfrom, int xto, int yfrom, int yto, int ybegin,
  1221. int pass, int sym)
  1222. {
  1223.    if (num_worklist >= MAXCALCWORK)
  1224.       return(-1);
  1225.    worklist[num_worklist].xxstart = xfrom;
  1226.    worklist[num_worklist].xxstop  = xto;
  1227.    worklist[num_worklist].yystart = yfrom;
  1228.    worklist[num_worklist].yystop  = yto;
  1229.    worklist[num_worklist].yybegin = ybegin;
  1230.    worklist[num_worklist].pass      = pass;
  1231.    worklist[num_worklist].sym      = sym;
  1232.    ++num_worklist;
  1233.    tidy_worklist();
  1234.    return(0);
  1235. }
  1236.  
  1237. static int _fastcall combine_worklist(void) /* look for 2 entries which can freely merge */
  1238. {
  1239.    int i,j;
  1240.    for (i=0; i<num_worklist; ++i)
  1241.       if (worklist[i].yystart == worklist[i].yybegin)
  1242.      for (j=i+1; j<num_worklist; ++j)
  1243.         if (worklist[j].sym == worklist[i].sym
  1244.         && worklist[j].yystart == worklist[j].yybegin
  1245.         && worklist[i].pass == worklist[j].pass)
  1246.         {
  1247.            if ( worklist[i].xxstart == worklist[j].xxstart
  1248.            && worklist[i].xxstop  == worklist[j].xxstop)
  1249.            {
  1250.           if (worklist[i].yystop+1 == worklist[j].yystart)
  1251.           {
  1252.              worklist[i].yystop = worklist[j].yystop;
  1253.              return(j);
  1254.           }
  1255.           if (worklist[j].yystop+1 == worklist[i].yystart)
  1256.           {
  1257.              worklist[i].yystart = worklist[j].yystart;
  1258.              worklist[i].yybegin = worklist[j].yybegin;
  1259.              return(j);
  1260.           }
  1261.            }
  1262.            if ( worklist[i].yystart == worklist[j].yystart
  1263.            && worklist[i].yystop  == worklist[j].yystop)
  1264.            {
  1265.           if (worklist[i].xxstop+1 == worklist[j].xxstart)
  1266.           {
  1267.              worklist[i].xxstop = worklist[j].xxstop;
  1268.              return(j);
  1269.           }
  1270.           if (worklist[j].xxstop+1 == worklist[i].xxstart)
  1271.           {
  1272.              worklist[i].xxstart = worklist[j].xxstart;
  1273.              return(j);
  1274.           }
  1275.            }
  1276.         }
  1277.    return(0); /* nothing combined */
  1278. }
  1279.  
  1280. void tidy_worklist(void) /* combine mergeable entries, resort */
  1281. {
  1282.    int i,j;
  1283.    WORKLIST tempwork;
  1284.    while ((i=combine_worklist()) != 0)
  1285.    { /* merged two, delete the gone one */
  1286.       while (++i < num_worklist)
  1287.      worklist[i-1] = worklist[i];
  1288.       --num_worklist;
  1289.    }
  1290.    for (i=0; i<num_worklist; ++i)
  1291.       for (j=i+1; j<num_worklist; ++j)
  1292.      if (worklist[j].pass < worklist[i].pass
  1293.          || (worklist[j].pass == worklist[i].pass
  1294.          && (worklist[j].yystart < worklist[i].yystart
  1295.          || (   worklist[j].yystart == worklist[i].yystart
  1296.          && worklist[j].xxstart <  worklist[i].xxstart))))
  1297.      { /* dumb sort, swap 2 entries to correct order */
  1298.         tempwork = worklist[i];
  1299.         worklist[i] = worklist[j];
  1300.         worklist[j] = tempwork;
  1301.      }
  1302. }
  1303.  
  1304.  
  1305. void get_julia_attractor (double real, double imag)
  1306. {
  1307.    _LCMPLX lresult;
  1308.    _CMPLX result;
  1309.    int savper;
  1310.    long savmaxit;
  1311.    int i;
  1312.  
  1313.    if (attractors == 0 && finattract == 0) /* not magnet & not requested */
  1314.       return;
  1315.  
  1316.    if (attractors >= N_ATTR)     /* space for more attractors ?  */
  1317.       return;               /* Bad luck - no room left !    */
  1318.  
  1319.    savper = periodicitycheck;
  1320.    savmaxit = maxit;
  1321.    periodicitycheck = 0;
  1322.    old.x = real;            /* prepare for f.p orbit calc */
  1323.    old.y = imag;
  1324.    tempsqrx = sqr(old.x);
  1325.    tempsqry = sqr(old.y);
  1326.  
  1327.    lold.x = (long)real;        /* prepare for int orbit calc */
  1328.    lold.y = (long)imag;
  1329.    ltempsqrx = (long)tempsqrx;
  1330.    ltempsqry = (long)tempsqry;
  1331.  
  1332.    lold.x = lold.x << bitshift;
  1333.    lold.y = lold.y << bitshift;
  1334.    ltempsqrx = ltempsqrx << bitshift;
  1335.    ltempsqry = ltempsqry << bitshift;
  1336.  
  1337.    if (maxit < 500)        /* we're going to try at least this hard */
  1338.       maxit = 500;
  1339.    color = 0;
  1340.    while (++color < maxit)
  1341.       if(curfractalspecific->orbitcalc())
  1342.      break;
  1343.    if (color >= maxit)        /* if orbit stays in the lake */
  1344.    {
  1345.       if (integerfractal)   /* remember where it went to */
  1346.      lresult = lnew;
  1347.       else
  1348.      result =  new;
  1349.      for (i=0;i<10;i++) {
  1350.       if(!curfractalspecific->orbitcalc()) /* if it stays in the lake */
  1351.       {                /* and doen't move far, probably */
  1352.      if (integerfractal)   /*   found a finite attractor    */
  1353.      {
  1354.         if(labs(lresult.x-lnew.x) < lclosenuff
  1355.         && labs(lresult.y-lnew.y) < lclosenuff)
  1356.         {
  1357.            lattr[attractors] = lnew;
  1358.            attrperiod[attractors] = i+1;
  1359.            attractors++;   /* another attractor - coloured lakes ! */
  1360.            break;
  1361.         }
  1362.      }
  1363.      else
  1364.      {
  1365.         if(fabs(result.x-new.x) < closenuff
  1366.         && fabs(result.y-new.y) < closenuff)
  1367.         {
  1368.            attr[attractors] = new;
  1369.            attrperiod[attractors] = i+1;
  1370.            attractors++;   /* another attractor - coloured lakes ! */
  1371.            break;
  1372.         }
  1373.      }
  1374.       } else {
  1375.       break;
  1376.       }
  1377.      }
  1378.    }
  1379.    if(attractors==0)
  1380.       periodicitycheck = savper;
  1381.    maxit = savmaxit;
  1382. }
  1383.  
  1384.  
  1385. #define maxyblk 7    /* must match calcfrac.c */
  1386. #define maxxblk 202  /* must match calcfrac.c */
  1387. int ssg_blocksize(void) /* used by solidguessing and by zoom panning */
  1388. {
  1389.    int blocksize,i;
  1390.    /* blocksize 4 if <300 rows, 8 if 300-599, 16 if 600-1199, 32 if >=1200 */
  1391.    blocksize=4;
  1392.    i=300;
  1393.    while(i<=ydots)
  1394.    {
  1395.       blocksize+=blocksize;
  1396.       i+=i;
  1397.    }
  1398.    /* increase blocksize if prefix array not big enough */
  1399.    while(blocksize*(maxxblk-2)<xdots || blocksize*(maxyblk-2)*16<ydots)
  1400.       blocksize+=blocksize;
  1401.    return(blocksize);
  1402. }
  1403.